package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.AgentInOutMoney;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.Agency;
import cc.yugu.yhdw.common.domain.hdw.AgencyInOutMoney;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.basic.AgentInOutMoneyService;
import cc.yugu.yhdw.webserver.service.hdw.AgencyInOutMoneyService;
import cc.yugu.yhdw.webserver.service.hdw.AgencyService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 代理商控制类
 * 
 * @Date:Created in 2018/8/17 16:09
 * @Author:
 * @Modified By: zjn 2018/8/17 16:09
 */ 
@Controller
@RequestMapping(value = "/security/hdw/agency")
public class AgencyController extends SecurityController {

    @Autowired
    AgencyService agencyService;
    @Autowired
    AgencyInOutMoneyService agencyInOutMoneyService;
    /**
     * 代理商下拉框事件
     * @Param [dummy, agentId, parentId, showAgency, response]
     * @Return void
     * @Author zxj 2018/7/27
     */
    @ResponseBody
    @ViewModel(ViewModel.JSON_ARRAY)
    @RequestMapping(value = "tree.htm")
    public void tree(String dummy, Integer agentId, Integer parentId, Integer showAgency, HttpServletResponse response) throws Exception {
        response.setContentType(ConstEnum.ContentType.JSON.getValue());
        agencyService.tree(dummy, agentId, parentId, showAgency != null && showAgency == 1, response.getOutputStream());
    }

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_01_02.getValue());
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(Agency search, HttpServletRequest request) {
//        if (getSessionUser(request).getType() == User.Type.SUPPLIER.getValue()) {
//            return null;
//        } else
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        } else if (getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            search.setId(getSessionUser(request).getAgencyId());
        }
        return PageResult.successResult(agencyService.findPage(search));
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("add.htm")
    public void add() {
    }

    @RequestMapping(value = "edit.htm")
    public String edit(Model model, int id) {
        Agency entity = agencyService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("id", id);
        }
        return "/security/hdw/agency/edit";
    }

    @RequestMapping(value = "edit_basic_info.htm")
    public String editBasicInfo(Model model, int id) {
        Agency entity = agencyService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/agency/edit_basic_info";
    }

    @RequestMapping("update_basic_info.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult updateBasicInfo(Agency entity) throws IOException {
        return agencyService.update(entity);
    }

    @RequestMapping(value = "edit_agency_deduct_money.htm")
    public String editAgencyDeductMoney(Model model, int id) {
        Agency entity = agencyService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("agencyId", id);
        }
        return "/security/hdw/agency/edit_agency_deduct_money";
    }


    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "create.htm")
    public ExtResult create(Agency entity) {
        return agencyService.insert(entity);
    }
    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "delete.htm")
    public ExtResult delete(int id) {
        return agencyService.delete(id);
    }
    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "disable.htm")
    public ExtResult disable(int id,int activeFlag) {
        return agencyService.disable(id,activeFlag);
    }

    @RequestMapping(value = "view.htm")
    public void view(Model model, int id, Integer item) {
        model.addAttribute("id", id);
        model.addAttribute("item", item);
    }

    @RequestMapping(value = "view_basic_info.htm")
    public String viewBasicInfo(Model model, int id) {
        //Agency entity = agencyService.find(id);
        Double sumAgencyMoney1=0.0;
        Double sumAgencyMoney2=0.0;
        // 查询代理商流水表中的按次换电总收入
        List<AgencyInOutMoney> agencyInOutMoneyHuan =agencyInOutMoneyService.find(id,AgencyInOutMoney.BizType.CHARGE_INCOME.getValue());
        if (agencyInOutMoneyHuan == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        // 遍历总收入
        for(AgencyInOutMoney huan : agencyInOutMoneyHuan){
            // 转成double型
            Double money1 = huan.getMoney()!=null?Double.parseDouble(huan.getMoney().toString()):0.0;
            // 累计求和
            sumAgencyMoney1+=money1;
        }
        model.addAttribute("sumAgencyMoney1",sumAgencyMoney1);
        // 查询代理商流水表中的套餐总收入
        List<AgencyInOutMoney> agencyInOutMoneyTaoCan = agencyInOutMoneyService.find(id,AgencyInOutMoney.BizType.PACKET_INCOME.getValue());
        if (agencyInOutMoneyTaoCan == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        for(AgencyInOutMoney taocan : agencyInOutMoneyTaoCan){
            Double money2 = taocan.getMoney()!=null?Double.parseDouble(taocan.getMoney().toString()):0.0;
            // 套餐求和
            sumAgencyMoney2+=money2;
        }
        model.addAttribute("sumAgencyMoney2",sumAgencyMoney2);

        Agency entity = agencyService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/agency/view_basic_info";
    }

    @RequestMapping(value = "view_in_out_money.htm")
    public void viewInOutMoney(Model model, int id) {
        model.addAttribute("agencyId", id);
    }

}
